<template>
  <div class="subject-container">
    <el-card class="box-card">
        <div class="search">
          <span>年级</span>
          <el-select 
            v-model="query.level" 
            style="width:300px;margin-left:5px;"
            clearable
            @clear="getSubjectList">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <el-button type="primary" class="searchBtn" @click="getSubjectList">查询</el-button>
          <el-button type="primary" @click="addDialogVisible = true" icon="el-icon-plus">添加</el-button>
      </div>
      <div class="table"  style="margin-top: 20px" >
          <el-table
            width="100%"
            :data="subjectList"
            stripe border>
            <el-table-column
              prop="id"
              label="id">
            </el-table-column>
            <el-table-column
              prop="name"
              label="学科">
            </el-table-column>
            <el-table-column
                prop="levelName"
                label="年级">
              </el-table-column>
            <el-table-column label="操作" >
                <template slot-scope="scope">
                  <div class="btns">
                    <el-button class="btn" @click="edit(scope.row.id)">编辑</el-button>
                    <el-button class="btn" @click="deleteSubject(scope.row.id)">删除</el-button>
                  </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              class="pagination"
              :current-page="query.pageIndex"
              :page-sizes="[1, 2, 5, 10]"
              :page-size="query.pageSize"
               layout="total, sizes, prev, pager, next, jumper"
              :total="total">
        </el-pagination>
      </div>
    </el-card>
    <el-dialog
        title="学科信息编辑"
        :visible.sync="addDialogVisible"
        :modal-append-to-body="false"
        @close="addDialogClosed"
        width="60%">
          <!-- 内容主体区域 -->
          <el-form ref="addFormRef" :rules="addFormRules" :model="addForm"  label-width="80px">
              <!-- 真实姓名 -->
              <el-form-item label="学科" prop="name">
                  <el-input v-model="addForm.name" style="width:100%"></el-input>
              </el-form-item>
              <!-- 年级 -->
              <el-form-item label="年级" prop="level">
                  <el-select v-model="addForm.level" style="width:100%">
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
              </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button class="saveBtn" type="primary" @click="submitForm">保存</el-button>
        </span>
     </el-dialog>

     <el-dialog
        title="学科信息编辑"
        :visible.sync="editDialogVisible"
        :modal-append-to-body="false"
        @close="editDialogClosed"
        width="60%">
          <!-- 内容主体区域 -->
          <el-form ref="editFormRef" :rules="editFormRules" :model="editForm"  label-width="80px">
              <!-- 真实姓名 -->
              <el-form-item label="学科" prop="name">
                  <el-input v-model="editForm.name" style="width:100%"></el-input>
              </el-form-item>
              <!-- 年级 -->
              <el-form-item label="年级" prop="level">
                  <el-select v-model="editForm.level" style="width:100%">
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
              </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button class="saveBtn" type="primary" @click="update">保存</el-button>
        </span>
     </el-dialog>
  </div>
</template>

<script>
export default {
  data(){
    return {
      editDialogVisible:false,
      editForm:{
        id: "",
        name: "",
        level: "",
        levelName: "",
        itemOrder: "",
        deleted: ""
      },
      //添加表单验证规则
      editFormRules:{
            //验证用户名是否合法
            name: [
                { required: true, message: "请输入用户名", trigger: "blur" }
            ]
      },
      addDialogVisible:false,
      //添加对象
      addForm:{
        name:"", //学科名称
        level: ""//年级
      },
      //添加表单验证规则
      addFormRules:{
            //验证用户名是否合法
            name: [
                { required: true, message: "请输入用户名", trigger: "blur" }
            ],
            level: [
                { required: true, message: "请选择年级", trigger: "blur" }
            ]
      },
       //查询对象
      query: {
          pageIndex: 1,
          pageSize: 5,
          level: ""
      },
      //表格数据
      subjectList: [],
      //总数
      total:0,
      //年级选择
      options: [{
          value: '1',
          label: '一年级'
        }, {
          value: '2',
          label: '二年级'
        }, {
          value: '3',
          label: '三年级'
        }, {
          value: '4',
          label: '四年级'
        }, {
          value: '5',
          label: '五年级'
        }, {
          value: '6',
          label: '六年级'
        }, {
          value: '7',
          label: '初一'
        }, {
          value: '8',
          label: '初二'
        }, {
          value: '9',
          label: '初三'
        }, {
          value: '10',
          label: '高一'
        }, {
          value: '11',
          label: '高二'
        }, {
          value: '12',
          label: '高三'
        }],
    }
  },
  created(){
    //获取用户列表
    this.getSubjectList();
  },
  methods:{
    //删除操作
    deleteSubject(id){
      this.$confirm('此操作将删除该学科, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            this.deleteRequest("/api/admin/subject/delete/"+id).then(resp=>{
              if(resp.code == 200){ //禁用成功
                //重新获取用户列表
                this.getSubjectList();
              }
            })
        }).catch(() => {
            this.$message({
                type: 'info',
                message: '已取消操作'
            });          
        });
    },
    //编辑操作
    edit(id){
      this.getRequest("/api/admin/subject/select/"+id).then( resp => {
        if(resp.code == 200){
          let newObj ={
              id: resp.obj.id,
              name: resp.obj.name,
              level: resp.obj.level+''
          }
          this.editForm = newObj;
          this.editDialogVisible = true;
        }
      });
    },
    //更新操作
    update(){
      this.$refs.editFormRef.validate(valid => {
         if(!valid){
            this.$message.error("请输入所有必填字段！！");
            return false;
         }else{
           this.putRequest("/api/admin/subject/edit",this.editForm).then(resp => {
             if(resp.code == 200){
                this.editDialogVisible = false;
                this.getSubjectList();
             }
           })
         }
      })
    },
    //添加操作
    submitForm(){
       this.$refs.addFormRef.validate( valid => {
         if(!valid){
            this.$message.error("请输入所有必填字段！！");
            return false;
         }else{
            this.postRequest("/api/admin/subject/edit",this.addForm).then( resp => {
              console.log(resp);
              this.addDialogVisible = false;
              this.getSubjectList();
            })
         }
       })
    },
    //重置操作
    addDialogClosed(){
      this.$refs.addFormRef.resetFields();
    },
     //重置操作
    editDialogClosed(){
      this.$refs.editFormRef.resetFields();
    },
     //获取用户列表
    async getSubjectList(){
      
      const resp = await this.getRequest("/api/admin/subject/page",{
        pageIndex: this.query.pageIndex,
        pageSize: this.query.pageSize,
        level: this.query.level
      });

      this.subjectList = resp.data;
      this.total = resp.total
    },
    //监听pagesize改变的事件
    handleSizeChange(newSize) {
        //console.log(newSize);
        this.query.pageSize = newSize
        this.getSubjectList();
    },
    //监听页码值改变的事件
    handleCurrentChange(newPage) {
        //console.log(newPage);
        this.query.pageIndex = newPage
        this.getSubjectList();
    } 
  }
}
</script>

<style lang="scss" scoped>
.subject-container{
.box-card{
     
      border-radius: 10px;
      .search{
        margin-top: 10px;
        display: flex;
        align-items: center;
        color:#1A1A1A;
        font-family: "黑体";
        .searchInput{
          margin-left: 10px;
          border:1px solid #1980FF;
          border-radius: 4px;
        }
        .searchBtn{
          margin: 0 0 0 20px;
        }
      }
      .table{
         color:#1A1A1A;
         font-family: "华文楷体";
         width: 100%;
         //内容居中
         /deep/.el-table th > .cell {
              text-align: center;
          }
          
          /deep/.el-table .cell {
              text-align: center;
          } 
          .btns{
            display: flex;
            justify-content: space-around;
          }
          .btn{
            border: none;
            font-size: 12px;
            margin:0 10px;
            padding: 0;
            cursor: pointer;
            &:nth-child(1){
              color:#1980FF;
            }
            &:nth-child(2){
              color:#F56C6C;
            }
          }
      }
      .pagination{
        margin: 20px 0 0 0;
        display: flex;
        justify-content: flex-end;
      }
    }
    .el-row{
      margin:0;
      font-size: 14px;
      color:#1A1A1A;
      font-family: "华文楷体";
    }
    .el-col{
      padding: 0 10px;
    }
   
    .saveBtn{
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      background: #66BB6A;
      margin-bottom: 20px;

    }
   
    /deep/.el-form-item {
      display: flex;
      flex-direction: column;
      margin-bottom: 10px !important;
    }
    /deep/.el-form-item__label{
      text-align: left;
    }
    /deep/.el-form-item__label-wrap {
      margin-left:  0 !important;
    }
    /* 选中后的字体颜色 */
    /deep/.el-radio__input.is-checked + .el-radio__label {
      color: #66BB6A !important;
    }
    /* 选中后小圆点的颜色 */
    /deep/.el-radio__input.is-checked .el-radio__inner {
      background: #66BB6A !important;
      border-color: #66BB6A !important;
    }
}
</style>